//
//  QLPersonalTailorVC.m
//  神约
//
//  Created by qlwl on 17/2/16.
//  Copyright © 2017年 qlwl. All rights reserved.
//

#import "QLPersonalTailorVC.h"
#import "QLPublishClassifyModel.h"
#import "WHC_ModelSqlite.h"

#define CellSpace 8
#define CellWidth (iphoneWidth - 100 - 2*8 - (3 + 1)* CellSpace)/3
#define CellHeight (CellWidth)

#define CellWidth2 (iphoneWidth - 2*10 - (3)* 16)/3
#define CellHeight2 (60)

@interface QLPersonalTailorVC ()
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView1;
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView2;

@end

@implementation QLPersonalTailorVC
{
    NSMutableArray * _dataArray;
    QLPublishClassifyModel * _selectModel;
    NSMutableArray * _selectArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.tableView.tableFooterView = [UIView new];
    ((UICollectionViewFlowLayout *)self.collectionView1.collectionViewLayout).minimumInteritemSpacing = CellSpace;
    ((UICollectionViewFlowLayout *)self.collectionView1.collectionViewLayout).minimumLineSpacing = 4;
    ((UICollectionViewFlowLayout *)self.collectionView2.collectionViewLayout).minimumLineSpacing = 16;
    [self netWorking];
    _selectArray = [[NSMutableArray alloc] init];
    [_selectArray addObjectsFromArray:[self getDatas]];
}

#pragma mark saveData
- (void)saveDatas:(NSArray *)datas finish:(void(^)(void))finish
{
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        [WHC_ModelSqlite clear:[QLPublishClassifyModel class]]; // 存之前，先删除
        [WHC_ModelSqlite inserts:datas];
        dispatch_async(dispatch_get_main_queue(), ^{
            if (finish) {
                finish();
            }
        });
    });
}
- (NSArray *)getDatas
{
    NSArray * array = [WHC_ModelSqlite query:[QLPublishClassifyModel class]];
    return array;
}

- (IBAction)cancelBtnClick:(id)sender {
    [_selectArray removeAllObjects];
    [self.collectionView2 reloadData];
}
- (IBAction)sureBtnClick:(id)sender {
    [self saveDatas:_selectArray finish:^{
        [self.navigationController popViewControllerAnimated:YES];
        if (self.changeSelectItem) {
            self.changeSelectItem();
        }
    }];
}


#pragma mark TabDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * cellName = @"cell";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellName];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellName];
    }
    QLPublishClassifyModel * model = _dataArray[indexPath.row];
    cell.textLabel.text = model.name;
    
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 40;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    QLPublishClassifyModel * model = _dataArray[indexPath.row];
    _selectModel = model;
    [self.collectionView1 reloadData];
}


#pragma mark CollectionViewDelegate
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (collectionView == self.collectionView1) {
        if (_selectModel) {
            return _selectModel.childlist.count;
        }
    }else if (collectionView == self.collectionView2){
        return _selectArray.count;
    }
    return 0;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"QLPersonalTailorCollCell" forIndexPath:indexPath];
    UILabel * label = [cell viewWithTag:10];
    if (collectionView == self.collectionView1) {
        QLPublishClassifyModel * model = _selectModel.childlist[indexPath.row];
        label.text = model.name;
    }else if (collectionView == self.collectionView2){
        QLPublishClassifyModel * model = _selectArray[indexPath.row];
        label.text = model.name;
    }
    return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (collectionView == self.collectionView1) {
        return CGSizeMake(CellWidth, CellHeight);
    }else{
        return CGSizeMake(CellWidth2, CellHeight2);
    }
    
}
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    if (collectionView == self.collectionView1) {
        return UIEdgeInsetsMake(10,CellSpace,0,CellSpace);
    }else{
        return UIEdgeInsetsMake(0,CellSpace,0,CellSpace);
    }
    
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (collectionView == self.collectionView1) {
        QLPublishClassifyModel * model = _selectModel.childlist[indexPath.row];
        if (_selectArray.count <= 3) { // 设置最大存储的元素
            
            [_selectArray addObject:model];
            [self.collectionView2 reloadData];
        }
    }
}
- (BOOL) shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender
{
    return YES;
}

#pragma mark netWorking 
- (void) netWorking
{
    NSMutableDictionary * postDict = [[NSMutableDictionary alloc] init];
    postDict[@"type"] = @"1";
    [QLNetWorking postJSONWithUrl:QLPublishLifeClissifyURL parameters:postDict isJosnRequest:YES rogress:nil success:^(id result) {
        NSLog(@"%@",result);
        if ([QLNetWorking sucess:[result objectForKey:@"mobileHead"] and:@"code" EqualToString:@"success"]) {
            _dataArray = [QLPublishClassifyModel mj_objectArrayWithKeyValuesArray:[[result objectForKey:@"mobileBody"] objectForKey:@"data"]];
            [self.tableView reloadData];
            _selectModel = [_dataArray firstObject];
            [self.collectionView1 reloadData];
            
        }
    } fail:^(NSError *error) {
        
    }];
}

@end
